package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.*;
import com.jiawa.train.business.domain.Req.DailyTrainSeatQueryReq;
import com.jiawa.train.business.domain.Req.DailyTrainSeatSaveReq;
import com.jiawa.train.business.domain.resp.DailyTrainSeatQueryResp;
import com.jiawa.train.business.mapper.DailyTrainSeatMapper;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainSeatService {

    @Autowired
    private DailyTrainSeatMapper dailyTrainSeatMapper;
    @Autowired
    private TrainSeatService trainSeatService;
    @Autowired
    private TrainStationService trainStationService;

    public void save(DailyTrainSeatSaveReq req) {
        DailyTrainSeat dailyTrainSeat = new DailyTrainSeat();
        BeanUtils.copyProperties(req, dailyTrainSeat);
        if(ObjectUtil.isNull(req.getId())) {
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setCreateTime(new Date());
            dailyTrainSeat.setUpdateTime(new Date());
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        }else {
            dailyTrainSeat.setUpdateTime(new Date());
            dailyTrainSeatMapper.updateByPrimaryKeySelective(dailyTrainSeat);
        }
    }

    public PageResp<DailyTrainSeatQueryResp> queryList(DailyTrainSeatQueryReq req) {
        DailyTrainSeatExample example = new DailyTrainSeatExample();
        PageHelper.startPage(req.getPage(),req.getSize());
        List<DailyTrainSeat> list = dailyTrainSeatMapper.selectByExample(example);

        List<DailyTrainSeatQueryResp> queryResps = BeanUtil.copyToList(list, DailyTrainSeatQueryResp.class);

        PageInfo<DailyTrainSeat> pageInfo = new PageInfo<>(list);

        PageResp<DailyTrainSeatQueryResp> resp = new PageResp<>();
        resp.setTotal(pageInfo.getTotal());
        resp.setList(queryResps);
        return resp;
    }

    public void delete(Long id) {
        dailyTrainSeatMapper.deleteByPrimaryKey(id);
    }

    public void genDaily(Date date,String trainCode){
        //删除某日某次的车厢信息
        DailyTrainSeatExample example = new DailyTrainSeatExample();
        example.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode);

        dailyTrainSeatMapper.deleteByExample(example);

        List<TrainStation> trainStationList = trainStationService.selectByTrainCode(trainCode);
        String sell= StrUtil.fillBefore("",'0',trainStationList.size()-1);
        //查出某车次的所有车站信息
        List<TrainSeat> seatList = trainSeatService.selectByTrainCode(trainCode);
        if(CollUtil.isEmpty(seatList)){
            return;
        }

        for(TrainSeat trainSeat : seatList){
            DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(trainSeat, DailyTrainSeat.class);
            dailyTrainSeat.setUpdateTime(new Date());
            dailyTrainSeat.setCreateTime(new Date());
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setDate(date);
            dailyTrainSeat.setSell(sell);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        }
    }

    public int countSeat(Date date,String trainCode,String seatType){
        DailyTrainSeatExample seatExample = new DailyTrainSeatExample();
        seatExample.createCriteria().andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andSeatTypeEqualTo(seatType);
        long count = dailyTrainSeatMapper.countByExample(seatExample);
        if(count == 0L){
            return -1;
        }
        return (int) count;
    }


    public List<DailyTrainSeat> querySeat(Date date,String trainCode,Integer carriageIndex){
        DailyTrainSeatExample seatExample = new DailyTrainSeatExample();
        seatExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andCarriageIndexEqualTo(carriageIndex);
        return dailyTrainSeatMapper.selectByExample(seatExample);
    }

    public void update(DailyTrainSeat seat) {
        dailyTrainSeatMapper.updateByPrimaryKeySelective(seat);
    }
}
